package manage.model;

import m.common.model.DictionaryModel;
import m.common.model.FieldMeta;
import m.common.model.LinkTableMeta;
import m.common.model.TableMeta;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.system.cache.FlushCache;
import m.system.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

@TableMeta(name="os_admin_group",description="管理员组表")
public class AdminGroup extends StatusModel implements DictionaryModel, FlushCache {
	private static final long serialVersionUID = 1L;
	
	@FieldMeta(name="name",type=FieldType.STRING,length=20,description="名称")
	private String name;
	@FieldMeta(name="num",type=FieldType.STRING,length=20,description="编号")
	private String num;
	@FieldMeta(name="description",type=FieldType.STRING,length=1000,description="描述")
	private String description;
	@FieldMeta(name="type",type=FieldType.STRING, defaultValue="A",description="类型",dictOption = "A:组,B:角色,C:机构")
	private String type;
	@FieldMeta(name="business",type=FieldType.STRING, defaultValue="A",description="类型",dictOption = "A:系统,B:业务")
	private String business;
	@FieldMeta(name="sort",type=FieldType.INT,description="排序")
	private Integer sort;

	@LinkTableMeta(name="parent_oid",table=AdminGroup.class,description="父管理员组")
	private AdminGroup parent;
	@LinkTableMeta(name="org_oid",table=OrgGroupView.class,description="所属机构")
	private OrgGroupView org;
	@LinkTableMeta(name="dept_oid",table=OrgGroupView.class,description="所属部门")
	private OrgGroupView dept;
	
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public Integer getSort() {
		return sort;
	}
	public AdminGroup getParent() {
		return parent;
	}
	public void setParent(AdminGroup parent) {
		this.parent = parent;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getBusiness() {
		return business;
	}
	public void setBusiness(String business) {
		this.business = business;
	}
	public void setSort(Integer sort) {
		this.sort = sort;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}

	public OrgGroupView getOrg() {
		return org;
	}

	public void setOrg(OrgGroupView org) {
		this.org = org;
	}

	public OrgGroupView getDept() {
		return dept;
	}

	public void setDept(OrgGroupView dept) {
		this.dept = dept;
	}

	@Override
	public String getDictValue() {
		return this.getOid();
	}

	@Override
	public String getDictName() {
		return this.name;
	}

	@Override
	public String getDictParentOid() {
		return null!=this.parent&&!StringUtil.isSpace(this.parent.getOid())?this.parent.getOid():"";
	}

	@Override
	public String getDictStatus() {
		return this.getStatus().equals("0")?"Y":"N";
	}

	@Override
	public AdminGroup getCacheModel(String key) throws Exception {
		return ModelQueryList.getModel(AdminGroup.class,new String[]{"*"},
				QueryCondition.eq("num",key));
	}

	@Override
	public List<AdminGroup> getCacheList(String key) throws Exception {
		String[] arr=key.split(";");//key格式  type属性;父oid或者oid多个英文逗号分隔;business属性
		List<QueryCondition> cons= new ArrayList<>();
		cons.add(QueryCondition.eq("type",arr[0]));
		if(arr.length>1&& !StringUtil.isSpace(arr[1])){
			String[] orgs=arr[1].split(",");
			List<QueryCondition> cs= new ArrayList<>();
			for(String org :orgs){
				cs.add(QueryCondition.eq("org.oid",org));
				cs.add(QueryCondition.eq("dept.oid",org));
				cs.add(QueryCondition.eq("oid",org));
			}
			cons.add(QueryCondition.or(cs.toArray(new QueryCondition[]{})));
		}
		if(arr.length>2&& !StringUtil.isSpace(arr[2])){
			cons.add(QueryCondition.eq("business",arr[2]));
		}
		return ModelQueryList.getModelList(AdminGroup.class,new String[]{"oid","status","name","num","parent.oid","org.oid","dept.oid"},null,
			QueryCondition.and(cons.toArray(new QueryCondition[]{})), QueryOrder.asc("parent.sort"),QueryOrder.asc("sort"));
	}

}
